<template>
	<view class="login">
		<view class="form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="账号">
					<u-input v-model="form.username" trim placeholder="请输入账号" />
				</u-form-item>
				<u-form-item label="密码">
					<u-input v-model="form.password" trim placeholder="请输入密码" />
				</u-form-item>
				<u-button class="btn" type="primary" :ripple="true" @click="submit">登录</u-button>
				<view class="register u-text-center u-m-t-20" @click="getRegister">注册</view>
			</u-form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: ''
				}
			}
		},
		methods: {
			async submit(){
				try{
					const data = await this.$u.post('/user/login',this.form)
					this.$u.vuex('vuex_token',data.token)
					uni.reLaunch({
						url:'../todaybills/todaybills'
					})
				}catch(err){
					if(err.statusCode === 422){
						this.$u.toast('用户名不存在！');
					}else if(err.statusCode === 423){
						this.$u.toast('密码错误!');
					}
				}
				
			},
			getRegister() {
				this.$u.route({
					url: 'pages/user/register'
				})
			}
		}
	}
</script>

<style scoped>
	.form {
		margin: 30% 20rpx;
	}

	.btn {
		margin-top: 20rpx;
		width: 80%;
	}
</style>
